package com.jiyun.order.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.order.mapper.*;
import com.jiyun.order.service.IOrdersService;
import com.jiyun.orders.pojo.GoodsInfo;
import com.jiyun.orders.pojo.Orders;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.orders.vo.OrderWrapper;
import com.jiyun.orders.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单基础信息表 服务实现类
 * </p>
 *
 * @author 1823.
 * @since 2025-02-12
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private FeeInfoMapper feeInfoMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private PaymentMethodInfoMapper paymentMethodInfoMapper;
    @Autowired
    private VehicleDemandInfoMapper vehicleDemandInfoMapper;




//    @Override
//    public void addOrders(Orders orders) {
//        orders.setCreateTime(new Date());
//        orders.setOrderStatus(2);
////        orders.setUid(1);
////        orders.setFid(5);
////        orders.setPayId(3);
////        orders.setVehId(4);
////        orders.setGid(2);
//        orders.setRegId(1);
//        ordersMapper.addOrders(orders);
//
//    }

    @Override
    public PageInfo<Orders> getOrders(Integer pageNum, Integer pageSize,OrdersVo ordersVo) {
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> ordersList = ordersMapper.getOrders(ordersVo);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        return ordersPageInfo;
    }

    @Override
    public Orders getOrdersDetail(Integer id) {
        return ordersMapper.getOrdersDetail(id);
    }

    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public void addOrders(OrderWrapper orderWrapper) {
        Orders orders = orderWrapper.getFormData();
        orders.setCreateTime(new Date());
        orders.setOrderStatus(2);
        orders.setRegId(1);
        orders.setUid(1);
        List<GoodsInfo> goodsList = orderWrapper.getGoodsList();
        String s = addGoods(goodsList);
        vehicleDemandInfoMapper.addVehicleDemandInfo(orderWrapper.getVehicleInfo());
        feeInfoMapper.addFeeInfo(orderWrapper.getFeeInfo());
        ordersMapper.addOrders(orders);
        paymentMethodInfoMapper.addPaymentMethodInfo(orderWrapper.getPaymentMethodInfo());
    }

    private String addGoods(List<GoodsInfo> goodsList) {
        List<Integer> list = new ArrayList();
        goodsList.forEach(goodsInfo -> {
            goodsInfoMapper.insert(goodsInfo);
            list.add(goodsInfo.getId());
        });
        String result = list.stream().map(String::valueOf) .collect(Collectors.joining(","));
        return result;
    }

    @Override
    public void addOrder(Orders orders) {
        orders.setCreateTime(new Date());
        orders.setOrderStatus(2);
        orders.setRegId(1);
        ordersMapper.insert(orders);
    }


}
